// packageI/new_blind_box/bindBoxCard/swipe/index.js
var app = getApp();
Component({
  options: {
    styleIsolation: 'shared' 
  },
  properties: {
    homeIsShow:{
      type: Boolean,
      value: false,
    },
    goodsIsShow: {
      type: Boolean,
      value: true,
    },
    allDataList: {
      type: null,
      value: {},
    },
    activeIndex: {
      type: null,
      value: 0,
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 1,
    total: 3,
    bubbleData: [],
    posterShow: false, //海报
    posterImg: "",
    isMoreNum: false
  },
  observers: {
    'activeIndex'(data) {
      if(!this.data.homeIsShow) {
        clearInterval(this.data.timer);
        this.setData({
          bubbleData: []
        })
        this.initScatter();
      }
    },
  },
  lifetimes: {
    attached() {
      // 在组件实例刚刚被创建时执行
      clearInterval(this.data.timer)
      this.initScatter();
    },
  },
  methods: {
    // 初始化气泡图
    initScatter() {
      if(!this.data.allDataList.list) { return }
      let bubbleData = this.data.allDataList.list[this.data.activeIndex] ? this.data.allDataList.list[this.data.activeIndex].goods : [];
      let offsets = [100,220,10,320,170];
      bubbleData.forEach((v, i) => {
        v.top = "250rpx";
        v.left = offsets[i%5] * 1 + 160 + "rpx";
        v.width = 120;
        //气泡的动画时长
        v.time = 5 ;
      });

      let index = 0
      this.setData({
        isMoreNum: bubbleData.length < 5 ? true :false
      })
      let newData = []
      clearInterval(this.data.timer)
      this.data.timer = setInterval(() => {
        if(this.data.isMoreNum && index >= bubbleData.length) {
          index = 0;
          clearInterval(this.data.timer);
          return
        }else if (index >= bubbleData.length) {
          index = 0;
        }
        newData.push(bubbleData[index]);
        this.setData({
          bubbleData: newData
        })
        ++index
      }, 1000)

    },
    // 点击查看商品
    clickCheck() {
      wx.navigateTo({
        url: '/packageF/new_blind_box/blindPool/index?id='+ this.data.allDataList.list[this.data.activeIndex].id
      });
    },
    // 点击客服
    openService(){
      this.setData({
        showServicePopup:!this.data.showServicePopup
      });
    },
    // 跳转到盲盒
    goToMyBindBox() {
      wx.navigateTo({
        url: "/packageA/member/myOrder_v2/myOrder_v2?status=0&orderType=newBindBox&title="+'我的盲盒',
      });
    },
    // 点击海报
    postShow(){
      app._getNetWork({
        url: app.getNetAddresss('plugin.new-blind-box.frontend.index.poster'),
        data: { id : this.data.allDataList.list[this.data.activeIndex].id },
        success: ({data}) => {
          if(data.result) {
            this.setData({
              posterImg: data.data.poster,
              posterShow:true
            })
          }else {
            wx.showToast({
              icon: 'none',
              title: data.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      })
    },
    shareHint() {
      wx.showToast({
        icon: "none",
        title: '长按图片分享好友',
        duration: 3000,
      });
    },
    saveImg() {
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {
                console.log('授权成功');
              }
            });
          }
        }
      });
      var imgSrc = this.data.posterImg;
      wx.downloadFile({
        url: imgSrc,
        success: function (res) {
          //图片保存到本地
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function (data) {
              wx.showToast({
                title: '保存成功',
                icon: 'none',
                duration: 2000
              });
            },
            fail: function (err) {
              if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                console.log("当初用户拒绝，再次发起授权");
                wx.openSetting({
                  success(settingdata) {
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      wx.showToast({
                        icon: 'none',
                        title: '获取权限成功,再次长按保存图片',
                        duration: 1500
                      });
                    } else {
                      wx.showToast({
                        icon: 'none',
                        title: '获取权限失败,保存图片需要用户授权',
                        duration: 1500
                      });
                    }
                  }
                });
              }
            },
            complete(res) {
              console.log(res);
            }
          });
        }
      });
    },
    closePoster() {
      this.setData({ posterShow: false });
    },
    // 跳转到首页
    goHomePage() {
      clearInterval(this.data.timer);
      wx.navigateTo({
        url: "/packageF/new_blind_box/index?id="+ this.data.allDataList.list[this.data.activeIndex].id ,
      });
    },
  },

})